﻿#region

using System;
using System.Collections.Generic;
using MediaSage.Lib.Configuration;
using MediaSage.Lib.Database;
using MediaSage.Lib.Utilities;
using NHibernate;
using NHibernate.Criterion;

#endregion

namespace MediaSage.Lib.Plugin
{
    public class ConfigItemsHelper
    {
        #region Fields

        private readonly Logger logger;
        private Config config;

        #endregion Fields

        #region Constructors

        public ConfigItemsHelper(Config Config)
        {
            config = Config;
            logger = Config.Logger;
            logger.Log("MediaSage.Lib.Plugin.ConfigItems() Initialized");
        }

        #endregion Constructors

        #region Methods

        public void CreateCIEntry(Guid ProviderID, string VariableName, EValueType CIType, string VariableValue,
                                  string Description)
        {
            logger.Log(string.Format("Entering MediaSage.Lib.Plugin.ConfigItems.CreateCIEntry({0},{1},{2},{3},{4})",
                                     ProviderID.ToString(), VariableName, CIType.ToString(), VariableValue, Description));
            ISession session = DBUtil.GetSession();
            PluginEntity piE;
            using (session.BeginTransaction())
            {
                piE =
                    session.CreateCriteria(typeof(PluginEntity)).Add(Restrictions.Eq("ID", ProviderID)).UniqueResult
                        <PluginEntity>();
                if (piE == null)
                {
                    logger.Log("Could not find a plugin with ID: " + ProviderID.ToString());
                    logger.Log(
                        string.Format("Exiting MediaSage.Lib.Plugin.ConfigItems.CreateCIEntry({0},{1},{2},{3},{4})",
                                      ProviderID.ToString(), VariableName, CIType.ToString(), VariableValue, Description));
                    session.Close();
                    return;
                }
            }

            var piC = new PluginConfigurationItemEntity();
            piC.ProviderID = ProviderID;
            piC.Description = Description;
            piC.Value = VariableValue;
            piC.ValueType = CIType;
            piC.VariableName = VariableName;
            piC.ID = Guid.NewGuid();

            piE.ConfigurationItems.Add(piC);
            piE.SaveOrUpdate();

            logger.Log(string.Format("Exiting MediaSage.Lib.Plugin.ConfigItems.CreateCIEntry({0},{1},{2},{3},{4})",
                                     ProviderID.ToString(), VariableName, CIType.ToString(), VariableValue, Description));
        }

        public PluginConfigurationItemEntity GetCIByNameForPlugin(Guid ProviderID, string VariableName)
        {
            logger.Log(string.Format("Entering MediaSage.Lib.Plugin.ConfigItems.GetCIsForPlugin({0},{1})",
                                     ProviderID.ToString(), VariableName));
            ISession session = DBUtil.GetSession();
            using (session.BeginTransaction())
            {
                var piC =
                    (List<PluginConfigurationItemEntity>)
                    session.CreateCriteria(typeof(PluginConfigurationItemEntity)).Add(Restrictions.Eq("ProviderID",
                                                                                                       ProviderID)).List
                        <PluginConfigurationItemEntity>();
                if (piC != null)
                {
                    foreach (PluginConfigurationItemEntity pluginCI in piC)
                    {
                        if (pluginCI.VariableName.Equals(VariableName))
                        {
                            logger.Log(
                                string.Format(
                                    "Exiting MediaSage.Lib.Plugin.ConfigItems.GetCIByNameForPlugin({0},{1}) with value: {2}",
                                    ProviderID.ToString(), VariableName, pluginCI.Value));
                            return pluginCI;
                        }
                    }
                }
            }
            logger.Log(
                string.Format(
                    "Exiting MediaSage.Lib.Plugin.ConfigItems.GetCIByNameForPlugin({0},{1}) with value: null",
                    ProviderID.ToString(), VariableName));
            return null;
        }

        public List<PluginConfigurationItemEntity> GetCIsForPlugin(Guid ProviderID)
        {
            logger.Log(string.Format("Entering MediaSage.Lib.Plugin.ConfigItems.GetCIsForPlugin({0})",
                                     ProviderID.ToString()));
            ISession session = DBUtil.GetSession();
            using (session.BeginTransaction())
            {
                var piC =
                    (List<PluginConfigurationItemEntity>)
                    session.CreateCriteria(typeof(PluginConfigurationItemEntity)).Add(Restrictions.Eq("ProviderID",
                                                                                                       ProviderID)).List
                        <PluginConfigurationItemEntity>();
                if (piC != null)
                {
                    logger.Log(
                        string.Format("Exiting MediaSage.Lib.Plugin.ConfigItems.GetCIsForPlugin({0}), rc={1} items",
                                      ProviderID.ToString(), piC.Count));
                    return piC;
                }
            }

            logger.Log(string.Format("Exiting MediaSage.Lib.Plugin.ConfigItems.GetCIsForPlugin({0}), rc=null",
                                     ProviderID.ToString()));
            return null;
        }

        public EValueType? GetCIType(Guid ProviderID, string VariableName)
        {
            logger.Log("Entering MediaSage.Lib.Plugin.ConfigItems.GetCIType(" + ProviderID.ToString() + ", " +
                       VariableName + ")");
            ISession session = DBUtil.GetSession();
            using (session.BeginTransaction())
            {
                var piC =
                    session.CreateCriteria(typeof(PluginConfigurationItemEntity)).Add(Restrictions.Eq("VariableName",
                                                                                                       VariableName)).
                        Add(Restrictions.Eq("ProviderID", ProviderID)).UniqueResult<PluginConfigurationItemEntity>();
                if (piC != null)
                {
                    logger.Log(Logger.Levels.WARN,
                               "Plugin Configuration Item: " + VariableName + " not found for Provider ID: " +
                               ProviderID);
                    logger.Log("Exiting MediaSage.Lib.Plugin.ConfigItems.GetCIType(" + ProviderID.ToString() + ", " +
                               VariableName + "), rc==null");
                    return null;
                }
                logger.Log("Exiting MediaSage.Lib.Plugin.ConfigItems.GetCIType(" + ProviderID.ToString() + ", " +
                           VariableName + "), rc==" + piC.ValueType.ToString());
                return piC.ValueType;
            }
        }

        public void SetCIType(Guid ProviderID, string VariableName, EValueType CIType)
        {
            logger.Log("Entering MediaSage.Lib.Plugin.ConfigItems.SetCIType(" + ProviderID.ToString() + ", " +
                       VariableName + "," + CIType.ToString() + ")");
            ISession session = DBUtil.GetSession();
            using (session.BeginTransaction())
            {
                var piC =
                    session.CreateCriteria(typeof(PluginConfigurationItemEntity)).Add(Restrictions.Eq("VariableName",
                                                                                                       VariableName)).
                        Add(Restrictions.Eq("ProviderID", ProviderID)).UniqueResult<PluginConfigurationItemEntity>();
                if (piC != null)
                {
                    logger.Log(Logger.Levels.WARN,
                               "Plugin Configuration Item: " + VariableName + " not found for Provider ID: " +
                               ProviderID);
                    logger.Log("Exiting MediaSage.Lib.Plugin.ConfigItems.SetCIType(" + ProviderID.ToString() + ", " +
                               VariableName + "," + CIType.ToString() + ")");
                    return;
                }
                if (CIType != piC.ValueType)
                {
                    piC.ValueType = CIType;
                    piC.SaveOrUpdate();
                }
            }
            logger.Log("Exiting MediaSage.Lib.Plugin.ConfigItems.SetCIType(" + ProviderID.ToString() + ", " +
                       VariableName + "," + CIType.ToString() + ")");
        }

        #endregion Methods
    }
}